package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"go_study/user_web/globle"
	"go_study/user_web/initial"
	router2 "go_study/user_web/router"
	"github.com/gin-gonic/gin/binding"
	myValidator "go_study/user_web/validator"
)

func main() {
	router := gin.Default()
	apiGroup := router.Group("u/v1")
	router2.InitialUserRooter(apiGroup)
	// 初始化翻译
	if err := initial.InitTrans("zh"); err != nil {
		panic(err)
	}
	// 注册验证器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		_ = v.RegisterValidation("mobile", myValidator.ValidateMoble)
		_ = v.RegisterTranslation("mobile", globle.Trans, func(ut ut.Translator) error {
			return ut.Add("mobile", "{0} 非法的手机号码!", true) // see universal-translator for details
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mobile", fe.Field())
			return t
		})
	}

	logger, _ := zap.NewDevelopment()
	zap.ReplaceGlobals(logger)
	// 初始化配置文件
	initial.InitConfig()
	port := globle.ServiceConfig.Port
	zap.S().Infof("启动服务成功:%d", port)


	if err := router.Run(fmt.Sprintf(":%d", port)); err != nil {
		zap.S().Panic("启动失败", err.Error())
	}
}
